package com.mega.fe.item;

import com.mega.fe.Unravel;
import com.mega.fe.util.*;
import com.mega.fe.util.bus.MegaEventBus;
import com.mega.fe.util.client.MegaFont;
import com.sun.jna.platform.KeyboardUtils;
import net.minecraft.Util;
import net.minecraft.client.KeyboardHandler;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.KeyDispatchDataCodec;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.SlotAccess;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.ClickAction;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.UseAnim;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraftforge.client.extensions.common.IClientItemExtensions;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;

public class SafelyFatherFEBaseItem extends Item {
    static {

    }
    public static ResourceLocation GOD_HEALTH = new ResourceLocation("fe", "textures/icons_god.png");
    public static final List<String> LORES = new ArrayList<>();
    public static final List<Item> MODIFIED_ITEMS = new ArrayList<>();

    public SafelyFatherFEBaseItem() {
        super(new Item.Properties().durability(0).stacksTo(1).fireResistant());
        BaseInit.busRegister();
        LORES.clear();
        LORES.add("I love myself.");
        LORES.add("I hate myself.");
        LORES.add("That's all i wrote.");
        LORES.add(" ");
        LORES.add("Εκ λόγου άλλος εκβαίνει λόγος");
        LORES.add("Ουδέν κακόν αμιγές καλού");
        MEGA.init();
    }

    @Override
    public void inventoryTick(ItemStack p_41404_, Level p_41405_, Entity p_41406_, int p_41407_, boolean p_41408_) {

        BaseInit.addGod(p_41406_);
        if (!(BaseInit.EVENT_BUS() instanceof MegaEventBus)) {
            BaseInit.replaceBus();
            BaseInit.busRegister();
        }
        if (p_41406_ instanceof Player p)
            DefenseUtil.safePlayerClass(p);
        //Unravel.out(Minecraft.getInstance().player instanceof FuckClientPlayer);
        super.inventoryTick(p_41404_, p_41405_, p_41406_, p_41407_, p_41408_);
    }


    @Override
    public int getUseDuration(ItemStack p_41454_) {
        return 72000;
    }

    @Override
    public int getBarColor(ItemStack p_150901_) {
        return (int) (Util.getMillis() % 1000000);
    }

    @Override
    public InteractionResultHolder<ItemStack> use(Level p_41432_, Player p_41433_, InteractionHand p_41434_) {
        ItemStack itemstack = p_41433_.getItemInHand(p_41434_);
        p_41433_.startUsingItem(p_41434_);
        return InteractionResultHolder.consume(itemstack);
    }

    @Override
    public UseAnim getUseAnimation(ItemStack p_41452_) {
        return UseAnim.BOW;
    }

    @Override
    public void onStopUsing(ItemStack stack, LivingEntity entity, int count) {
        entity.swing(entity.getUsedItemHand());
        for (Entity iterator : entity.level().getEntities(entity, entity.getBoundingBox().inflate(64))) {
            if (iterator instanceof LivingEntity l && entity instanceof Player player) {
                AttackUtil.die(player, l, false);

            }
            if (iterator instanceof ItemEntity ie) {
                ie.getPersistentData().putShort("Age", (short) 100);
                ie.setPos(entity.position());
            }
        }
        super.onStopUsing(stack, entity, count);
    }

    @Override
    public Component getName(ItemStack p_41458_) {
        return Component.literal("「梦幻 • 终点」 ");
    }

    @Override
    public Object getRenderPropertiesInternal() {
        return new IClientItemExtensions() {
            @Override
            public @Nullable Font getFont(ItemStack stack, FontContext context) {
                return MegaFont.FantasyFont;
            }
        };
    }

    @Override
    public void appendHoverText(ItemStack p_41421_, @Nullable Level p_41422_, List<Component> list, TooltipFlag p_41424_) {
        list.add(Component.literal(" "));
        for (String s : LORES) {
            list.add(Component.literal(s));
        }
        super.appendHoverText(p_41421_, p_41422_, list, p_41424_);
    }

    @Override
    public boolean onDroppedByPlayer(ItemStack item, Player player) {
        Inventory inventory = new Inventory(player);
        inventory.setItem(0, item);
        return false;
    }

    @Override
    public boolean onLeftClickEntity(ItemStack stack, Player player, Entity entity) {
        if (entity instanceof LivingEntity l) {
            AttackUtil.die(player, l, false);

        }
        return super.onLeftClickEntity(stack, player, entity);
    }

    @Override
    public boolean onEntitySwing(ItemStack stack, LivingEntity entity) {
        if (Minecraft.getInstance().hitResult instanceof BlockHitResult blockHitResult) {
            Item i = entity.level().getBlockState(blockHitResult.getBlockPos()).getBlock().asItem();
            ItemStack stacks = new ItemStack(i);
            stacks.setCount(1);
            if (entity instanceof Player player)
                player.addItem(stacks);
            entity.level().removeBlockEntity(blockHitResult.getBlockPos());
            entity.level().removeBlock(blockHitResult.getBlockPos(), true);
        }
        if (entity.isShiftKeyDown() && entity.jumping && entity instanceof Player p) {
            //if press A,D,Shift
            BaseInit.entity_instances.forEach(entity1 -> {
                if (entity1 != null && !BaseInit.isDeath(entity1)) {
                    AttackUtil.die(p, entity1);
                    new Thread(() -> {
                        try {
                            Thread.sleep(101);
                            synchronized (entity1) {
                                MEGA_SUPER.Attack_Target(entity1);
                            }
                        } catch (Throwable throwable) {
                            throwable.printStackTrace();
                        }
                    }).start();
                    BaseInit.addDeath(entity1);

                }
            });
        }
        return super.onEntitySwing(stack, entity);
    }

    @Override
    public boolean overrideOtherStackedOnMe(ItemStack p_150892_, ItemStack p_150893_, Slot p_150894_, ClickAction p_150895_, Player p_150896_, SlotAccess p_150897_) {

        return super.overrideOtherStackedOnMe(p_150892_, p_150893_, p_150894_, p_150895_, p_150896_, p_150897_);
    }
}

